/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   14-12-24 下午2:03 Created by dwzhang
 */
package com.jade.bss.beacon.impl;

import java.util.List;

import javax.ejb.TransactionAttribute;
import javax.inject.Inject;
import javax.inject.Named;

import com.jade.bss.beacon.*;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.util.PagingList;
import org.springframework.transaction.annotation.Transactional;

/**
 * 型号管理器实现。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 14-12-24 下午2:03
 */
@Named ("bss_beaconModelManager")
public class ModelManagerImpl
        implements BeaconModelManager
{
    private BeaconModelDao modelDao;

    @Inject
    public void setModelDao(@Named ("bss_beaconModelDao") BeaconModelDao modelDao)
    {
        this.modelDao = modelDao;
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void addModel(BeaconModel model)
            throws BeaconException
    {
        //check if the model is existed
        if (getModel(model.getId()) != null) {
            throw new BeaconException(BeaconConstants.ERR_MODEL_EXISTED, "beacon model is already existed", null,
                    model.getId());
        }
        modelDao.addModel(model);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void updateModel(BeaconModel model)
            throws BeaconException
    {
        modelDao.updateModel(model);
    }

    public BeaconModel getModel(String id)
    {
        return modelDao.getModel(id);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void removeModel(String... ids)
            throws BeaconException
    {
        for (String id : ids) {
            modelDao.removeModel(id);
        }
    }

    public List<BeaconModel> listModel(Criteria criteria)
    {
        return modelDao.listModel(criteria);
    }

    public PagingList<BeaconModel> queryModel(Criteria criteria)
    {
        return modelDao.queryModel(criteria);
    }
}
